// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// L [author] rhyme_lph
// Q [email]  rhymelph@gmail.com
// M [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/chat_room_message_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/chat_room/left_chat_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/ace_virtual_assistant/notification_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "notificationPage")
class NotificationPage extends StatefulWidget {
  @override
  _NotificationPageState createState() => _NotificationPageState();
}

class _NotificationPageState extends State<NotificationPage>
    with PagePresenter<NotificationPage, NotificationProvider> {
  @override
  Widget get buildWidget => ColoredBox(
        color: Color.fromRGBO(249, 249, 249, 1),
        child: listBuilder<Tuple2<bool, int>>(
            selector: (_, __) => Tuple2(__.hasUp, __.data.length),
            builder:
                (BuildContext context, Tuple2<bool, int> value, Widget child) =>
                    SmartRefresher(
                      controller: provider.refreshController,
                      onRefresh: provider.onRefresh,
                      onLoading: provider.onLoadMore,
                      enablePullUp: value.item1,
                      child: ListView.builder(
                        padding: const EdgeInsets.only(bottom: 8.0),
                        itemBuilder: _buildItems,
                        itemCount: value.item2,
                      ),
                    )),
      );

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.notification,
      );

  Widget _buildItems(BuildContext context, int index) {
    return listBuilder<ChatRoomMessageEntity>(
      selector: (_, __) => __.data[index],
      builder:
          (BuildContext context, ChatRoomMessageEntity value, Widget child) {
        return LeftChatItem(
          avatar: value.avatar,
          contents: [value],
          time: DateTimeUtils.formatDateTimeChatRoom(value.createdAt),
          onAction: (action) {
            provider.onActionTo(
              action['type'],
              action['text'],
              action['data'],
            );
          },
        );
      },
    );
  }
}
